/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import javax.swing.ListModel;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;

/**
 *
 * @author minaev
 */
public class JListModelOutputStream extends OutputStream{
    
    private ArrayListModel<String> console = new ArrayListModel<String>();

    private int MAX_STR_LEN = 255;
    private byte[] in_buf = new byte[MAX_STR_LEN];
    private byte[] tmpbuf;
    private int c_in_buf = 0;
    
    @Override
    public void write(int b) throws IOException {
        if (b != 0xA) {
            if(b!=0xD){
                if(c_in_buf<MAX_STR_LEN){
                    in_buf[c_in_buf++] = (byte) b;
                }
            }
        } else {
            /*for(int i=c_in_buf;i<MAX_STR_LEN;i++){
                in_buf[i] = 0;
            }*/
            tmpbuf = new byte[c_in_buf];
            System.arraycopy(in_buf, 0, tmpbuf, 0, c_in_buf);
            addLine(new String(tmpbuf/*,"UTF-8"*/));
            tmpbuf = null;
            //System.out.println("out string: "+new String(in_buf,"UTF-8"));
            
            c_in_buf=0;
        }
    }

    private void addLine(String in_buf) {
        console.add(in_buf);
    }

    public ArrayListModel<String> getListModel() {
        return console;
    }

    public void clear() {
        console.clear();
    }
    
    
}
